'''
# Coding:utf-8
# Project: online-exam
# Author: rtf
# Time: 2022-07-26 09:32:50
# FileName: modify_password.py
# Software: PyCharm
'''

from funcs.user_info import get_uesr_info
from trump.query import modify_item, get_item
from trump.utils import ok, fail
from trump.decorators import table_headers
from utils.params_validate_decorator import validate_params


@validate_params([["old_password", str, True], ["password", str, True]])
async def post(app, request):
    params = request.json
    user_id, role_code = get_uesr_info(request)
    if role_code == "STUDENT":
        table_name = "student"
    elif role_code == "TEACHER":
        table_name = "teacher"
    else:
        table_name = "manager"
    user = await get_item(app.ctx.pool, table_name, user_id)
    save_password = app.ctx.fernet.decrypt(user.get("password"))
    if params.get("old_password") != save_password:
        return fail(return_msg="请输入正确的旧密码！")
    await modify_item(app.ctx.pool, table_name, user_id, {"password": app.ctx.fernet.encrypt(params.get("password"))})
    return ok(return_msg="修改成功！")


async def put(app, request, id):
    pass


@table_headers
async def ls(app, request):
    pass


async def get(app, request, id):
    pass


async def delete(app, request, id):
    pass
